<?php

abstract class Kd_Html_Element {

    private $_template = '';
    protected $attributes = array();
    protected $styles = array();
    protected $hidden = FALSE;
    protected $enabled = TRUE;
    protected $classes = array();
    protected $id = '';
    protected $parent = NULL;
    protected $settings = array();
    protected $templateName = '';
    protected $events = array();

    protected function __construct() {

    }

    public function getAttributes() {
        return $this->attributes;
    }

    public function getAttribute($name) {
        if (isset($this->attributes[$name])) {
            return $this->attributes[$name];
        }
        return '';
    }

    public function setAttribute($name, $value) {
        $this->attributes[$name] = $value;
        return $this;
    }

    public function getSettings() {
        return $this->settings;
    }

    public function getSetting($name) {
        return $this->settings[$name];
    }

    public function setSetting($name, $value) {
        $this->settings[$name] = $value;
        return $this;
    }

    public function bindEvent($event, $script) {
        $this->events[$event] = $script;
        return $this;
    }

    public function unbindEvent($event) {
        if (isset($this->events[$event])) {
            unsset($this->events[$event]);
        }
        return $this;
    }

    public function getClasses() {
        return $this->classes;
    }

    public function setClasses() {
        $this->classes = func_get_args();
        return $this;
    }

    public function addClass() {
        $arr = func_get_args();
        $this->classes = array_merge($this->classes, $arr);
        return $this;
    }

    public function getStyles() {
        return $this->styles;
    }

    public function setStyle($name, $value) {
        $this->styles[$name] = $value;
        return $this;
    }

    public function getTemplate() {
        if (empty($this->_template)) {
            foreach (Kd_Html_Config::getTemplatePaths() as $path) {
                if (file_exists($path . str_replace('/', DIRECTORY_SEPARATOR, $this->templateName) . '.phtml')) {
                    $this->_template = $path . str_replace('/', DIRECTORY_SEPARATOR, $this->templateName) . '.phtml';
                    break;
                }
            }
        }

        return $this->_template;
    }

    public function setTemplate($value) {
        $this->_template = $value;
        return $this;
    }

    public function getId() {
        return $this->getAttribute('id');
    }

    public function setId($value) {
        return $this->setAttribute('id', $value);
    }

    public function getTitle() {
        return $this->getAttribute('title');
    }

    public function setTitle($value) {
        return $this->setAttribute('title', $value);
    }

    public function getHidden() {
        return $this->hidden;
    }

    public function setHidden($value) {
        $this->hidden = $value;
        return $this;
    }

    public function getEnabled() {
        return $this->enabled;
    }

    public function setEnabled($value) {
        $this->enabled = $value;
        return $this;
    }

    public function toHtml() {
        $tmpl = $this->getTemplate();
        if (!empty($tmpl)) {
            ob_start();
            include $tmpl;
            return ob_get_clean();
        } else {
            return $this->render();
        }
    }

    public function getAttributeHtml() {
        $atts = array();
        foreach ($this->getAttributes() as $k => $v) {
            $atts[] = $k . '="' . $v . '"';
        }

        $style = $this->getStyleHtml();
        if (!empty($style)) {
            $atts[] = $style;
        }

        $setting = $this->getSettingHtml();
        if (!empty($setting)) {
            $atts[] = $setting;
        }

        $events = $this->getEventHtml();
        if (!empty($events)) {
            $atts[] = $events;
        }

        $class = $this->getClassHtml();
        if (!empty($class)) {
            $atts[] = $class;
        }

        if (!empty($atts)) {
            return ' ' . implode(' ', $atts);
        }
        return '';
    }

    public function getStyleHtml() {
        $atts = array();
        if ($this->getHidden()) {
            $this->setStyle('display', 'none');
        }
        if (!empty($this->styles)) {
            foreach ($this->getStyles() as $k => $v) {
                $atts[] = "$k:$v";
            }
        }
        if (!empty($atts)) {
            return 'style="' . implode(';', $atts) . '"';
        }
        return '';
    }

    public function getClassHtml() {
        if ($this->getEnabled() != TRUE) {
            $this->addClass('disabled');
        }
        $result = '';
        if (!empty($this->classes)) {
            $result = 'class="' . implode(' ', array_unique($this->classes)) . '"';
        }
        return $result;
    }

    public function getSettingHtml() {
        if (!empty($this->settings)) {
            return 'setting="' . Kd_Util_Json::encode($this->getSettings(), TRUE) . '"';
        }
        return '';
    }

    public function getEventHtml() {
        $result = '';
        if (!empty($this->events)) {
            $result = array();
            foreach ($this->events as $event => $script) {
                $result[] = "on$event=\"javascript:$script\"";
            }
            $result = implode(' ', $result);
        }
        return $result;
    }

    public function getType() {
        $classNames = explode('_', get_class($this));
        return strtolower(end($classNames));
    }

    abstract protected function render();
}